package com.company.permission.servlet;

import com.company.permission.dao.RoleDao;
import com.company.permission.entity.Role;

import com.company.permission.dao.RoleDao;
import com.company.permission.entity.Role;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 移除 @WebServlet 注解，在web.xml中配置
public class RoleServlet extends BaseServlet {

    private RoleDao roleDao = new RoleDao();

    /**
     * 任务二：查询特定角色及其所拥有的所有菜单权限
     */
    public void getRoleWithMenus(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String roleIdStr = req.getParameter("roleId");
        String roleKey = req.getParameter("roleKey");

        try {
            Role role = null;
            if (roleIdStr != null && !roleIdStr.trim().isEmpty()) {
                Long roleId = Long.parseLong(roleIdStr);
                role = roleDao.findRoleWithMenusById(roleId);
            } else if (roleKey != null && !roleKey.trim().isEmpty()) {
                role = roleDao.findRoleWithMenusByRoleKey(roleKey);
            } else {
                sendError(resp, "请提供roleId或roleKey参数");
                return;
            }

            if (role != null) {
                sendSuccess(resp, role);
            } else {
                sendError(resp, "角色不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            sendError(resp, "查询角色失败: " + e.getMessage());
        }
    }
}
